package com.sphd.emanu.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.interfaces.FoodNoListener;
import com.sphd.emanu.pojo.FoodEntity;
import com.sphd.emanu.pojo.TypeEntity;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author Hu
 * @Time 2017/4/26 --17:24.
 * @Email 87683202@qq.com
 */
public class ChooseTypeSectionAdapter extends SectionedBaseAdapter {

    Context context;
    List<TypeEntity> typeEntityList;
    private HolderClickListener mHolderClickListener;

    private FoodNoListener foodNoListener;

    public void setFoodNoListener(FoodNoListener callBackListener) {
        this.foodNoListener = callBackListener;
    }

    public ChooseTypeSectionAdapter(Context context, List<TypeEntity> typeEntityList) {
        this.context = context;
        this.typeEntityList = typeEntityList;
    }

    @Override
    public Object getItem(int section, int position) {
        return typeEntityList.get(section).getFoodEntityList().get(position);
    }

    @Override
    public long getItemId(int section, int position) {
        return position;
    }

    @Override
    public int getSectionCount() {
        return typeEntityList.size();
    }

    @Override
    public int getCountForSection(int section) {
        return typeEntityList.get(section).getFoodEntityList().size();
    }

    @Override
    public View getItemView(int section, final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_food, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TypeEntity typeEntity=typeEntityList.get(section);
        List<FoodEntity> foodEntityList =typeEntity.getFoodEntityList();
        final FoodEntity foodEntity =foodEntityList.get(position);
        //此处的记录section 和postion只为购物车内操作的时候对菜品总表进行最快的定位改变
        foodEntity.setSection(section);
        foodEntity.setRepos(position);
        viewHolder.foodName.setText(foodEntity.getDishesName());
        viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo() == 0 ? "" : foodEntity.getChooseNo()));
        //加
        viewHolder.increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = foodEntity.getChooseNo();
                num++;
                foodEntity.setChooseNo(num);
                viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo() == 0 ? "" : foodEntity.getChooseNo()));
                if (foodNoListener != null) {

                    foodNoListener.add(foodEntity, 1, position);

                    if(mHolderClickListener!=null){
                        int[] start_location = new int[2];
                        viewHolder.shoppingNum.getLocationInWindow(start_location);//获取点击商品图片的位置
                        Drawable drawable = context.getResources().getDrawable(R.drawable.adddetail);//复制一个新的商品图标
                        //TODO:解决方案，先监听到左边ListView的Item中，然后在开始动画添加
                        mHolderClickListener.onHolderClick(drawable, start_location);
                    }


                }
            }
        });
        //减
        viewHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = foodEntity.getChooseNo();
                if (num > 0) {
                    num--;
                    foodEntity.setChooseNo(num);
                    viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo() == 0 ? "" : foodEntity.getChooseNo()));
                    if (foodNoListener != null) {
                        foodNoListener.reduce(foodEntity, 1, position);
                    }
                }
            }
        });

        return convertView;

    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.section_heard, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        layout.setClickable(false);
        ((TextView) layout.findViewById(R.id.section_name)).setText(typeEntityList.get(section).getTypeName());
        return layout;
    }


    static class ViewHolder {
        @BindView(R.id.food_name)
        TextView foodName;
        @BindView(R.id.reduce)
        TextView reduce;
        @BindView(R.id.shoppingNum)
        TextView shoppingNum;
        @BindView(R.id.increase)
        TextView increase;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void SetOnSetHolderClickListener(HolderClickListener holderClickListener) {
        this.mHolderClickListener = holderClickListener;
    }

    public interface HolderClickListener {
        void onHolderClick(Drawable drawable, int[] start_location);
    }

}
